package com.alpha.eceasy.mall.controller;

import com.alpha.eceasy.mall.api.dto.MallCommodityDTO;
import com.alpha.eceasy.mall.api.entity.MallCommodity;
import com.alpha.eceasy.mall.api.vo.MallCommodityVO;
import com.alpha.eceasy.core.R;
import com.alpha.eceasy.mall.service.MallCommodityService;
import com.alpha.eceasy.mall.service.impl.MallCommodityServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * 商品表
 *
 * @author huangpengfei
 * @since 2022-02-11 16:55:59
 */
@RestController
@RequestMapping("/commodity")
@Tag(description = "commodity", name = "商品表")
public class MallCommodityController {
    @Resource
    private MallCommodityServiceImpl mallCommodityService;

    /**
     * 分页查询
     *
     * @param mallCommodity 商品
     * @return 分页结果
     */
    @Operation(description = "分页查询", summary = "分页查询")
    @GetMapping("")
    public R<IPage<MallCommodityVO>> getMallCommodityPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNumber,
                                                          @RequestParam(value = "pageSize", required = false, defaultValue = "1000") Integer pageSize,
                                                          MallCommodity mallCommodity) {
        return R.ok(mallCommodityService.getMallCommodityPage(Page.of(pageNumber, pageSize), mallCommodity));
    }

    /**
     * 通过guid查询商品详情
     *
     * @param guid guid
     * @return R 返回结果
     */
    @Operation(description = "通过guid查询商品详情", summary = "通过guid查询商品详情")
    @GetMapping("/{guid}")
    public R<MallCommodityVO> getByGuid(@PathVariable("guid") String guid) {
        return R.ok(mallCommodityService.getByGuid(guid));
    }

    /**
     * 通过guid删除商品
     *
     * @param guid guid
     * @return R 返回结果
     */
    //@SysLog("通过id删除商品")
    @Operation(description = "通过guid删除商品", summary = "通过guid删除商品")
    @DeleteMapping("/{guid}")
    public R<Boolean> removeById(@PathVariable String guid) {
        return R.ok(mallCommodityService.removeByGuid(guid));
    }

    /**
     * 新增商品
     *
     * @param dto 商品
     * @return R 返回结果
     */
    @Hidden
    @Operation(description = "新增商品", summary = "新增商品")
    //@SysLog("新增商品")
    @PostMapping
    public R<String> save(@RequestBody MallCommodityDTO dto) {
        return R.ok(mallCommodityService.save(dto));
    }

    /**
     * 修改商品
     *
     * @param mallCommodityVO 商品
     * @return R 返回结果 是否成功
     */
    @Operation(description = "通过guid修改商品", summary = "通过guid修改商品")
    //@SysLog("修改商品")
    @PutMapping
    public R<Boolean> updateByGuid(@RequestBody MallCommodityVO mallCommodityVO) {
        return R.ok(mallCommodityService.updateByGuid(mallCommodityVO));
    }
}
